//
//  ViewController.m
//  UI22-阿里星球（音乐）
//
//  Created by 韩旭 on 16/8/13.
//  Copyright © 2016年 lanou. All rights reserved.
//
#import "ViewController.h"
#import "AFNetworking.h"
#import "DataModel.h"
#import "SelfSizeTools.h"
#import "HX_RecmmentSong.h"
#import "HX_NewSongs.h"
#import "HX_PhoneSongs.h"
#import "HX_SoleHome.h"
#import "HX_CycleImage.h"
#import "DataSmallModel.h"
#import "HX_CustomCell.h"
#import "HX_DetailView.h"
#import "HX_ActionModel.h"
#define kTableView @"table"
#define kTableViewNewsSongs @"tableViewNewsSong"
#define kTableViewPhoneSongs @"tableViewPhoneSongs"
#define kTableViewHome @"tableHomeSole"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *arrTitleName;
@property (nonatomic, strong) UIView *viewHead;
@property (nonatomic, strong) HX_CycleImage *hx_CycleImage;
@property (nonatomic, strong) NSMutableArray *arrCycleImage;
@property (nonatomic, strong) NSMutableArray *arrayAliXingQiu;
@property (nonatomic, strong) NSMutableArray *arrValue;
@property (nonatomic, strong) UITapGestureRecognizer *tap;
@property (nonatomic, strong) UITapGestureRecognizer *tap1;
@property (nonatomic, strong) UITapGestureRecognizer *tap2;
@end

@implementation ViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (NSMutableArray *)arrTitleName {
    if (!_arrTitleName) {
        _arrTitleName = [NSMutableArray array];
    }
    return _arrTitleName;
}
- (NSMutableArray *)arrValue {
    if (!_arrValue) {
        _arrValue = [NSMutableArray array];
    }
    return _arrValue;
}
- (NSMutableArray *)arrCycleImage {
    if (!_arrCycleImage) {
        _arrCycleImage = [NSMutableArray array];
    }
    return _arrCycleImage;
}
- (NSMutableArray *)arrayAliXingQiu {
    if (!_arrayAliXingQiu) {
        _arrayAliXingQiu = [NSMutableArray array];
    }
    return  _arrayAliXingQiu;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.navigationItem.title = @"阿里星球";
    [self creatTableHeadView];
    [self creatTableView];
    [self getNetWorkintModel];
}
- (void)creatTableHeadView {
    self.viewHead = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 200)];
    _viewHead.backgroundColor = [UIColor whiteColor];
    self.hx_CycleImage = [[HX_CycleImage alloc] initWithFrame:CGRectMake(0, 0, kWidth, 200)];
    [_viewHead addSubview:_hx_CycleImage];
    [self.view addSubview:_viewHead];
}
- (void)getNetWorkintModel {
    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
    [manger GET:NETWORKING parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dicAll = responseObject;
        NSArray *arrData = dicAll[@"data"];
        for (NSDictionary *dic in arrData) {
            [self.arrTitleName addObject:[DataModel hx_BasemodelWithDic:dic]];
        }
        NSDictionary *dic = arrData[0];
        NSArray *arrDatas = dic[@"data"];
        for (NSDictionary *dicDatas in arrDatas) {
            DataSmallModel *dataSmall = dicDatas[@"picUrl"];
            [self.arrCycleImage addObject:dataSmall];
        }
        NSDictionary *dicData = arrData[1];
        NSArray *arrDataAli = dicData[@"data"];
        
        for (NSDictionary *dataAli in arrDataAli) {
            DataSmallModel *dataSmallAli = [[DataSmallModel alloc] init];
            dataSmallAli.name = dataAli[@"name"];
            dataSmallAli.picUrl = dataAli[@"picUrl"];
            [self.arrayAliXingQiu addObject:dataSmallAli];
        }
        NSString *firstImage = [_arrCycleImage firstObject];
        NSString *lastImage = [_arrCycleImage lastObject];
        [_arrCycleImage insertObject:lastImage atIndex:0];
        [_arrCycleImage addObject:firstImage];
        _hx_CycleImage.arrDataSource = _arrCycleImage;
        [_tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];
}

- (void)creatTableView {
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 64) style:0];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableHeaderView = _viewHead;
    [self.view addSubview:_tableView];
    [_tableView registerNib:[UINib nibWithNibName:@"HX_RecmmentSong" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kTableView];
    [_tableView registerNib:[UINib nibWithNibName:@"HX_NewSongs" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kTableViewNewsSongs];
    [_tableView registerNib:[UINib nibWithNibName:@"HX_PhoneSongs" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kTableViewPhoneSongs];
    [_tableView registerNib:[UINib nibWithNibName:@"HX_SoleHome" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kTableViewHome];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _arrTitleName.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    } else if (section == 1) {
        return 1;
    } else if (section == 2){
        return 1;
    } else if (section == 7){
        DataModel *model = _arrTitleName[section];
        return model.data.count;
    } else {
        return 1;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 2) {
        HX_RecmmentSong *cell = [tableView dequeueReusableCellWithIdentifier:kTableView];
        cell.selectionStyle = NO;
        cell.backgroundColor = [UIColor blackColor];
        DataModel *dataModel = _arrTitleName[indexPath.section];
        cell.dataSmallModel = dataModel.data[indexPath.row];
        cell.arrData = [dataModel.data mutableCopy];
        
        return cell;
    } else if (indexPath.section == 3 || indexPath.section == 6){
        HX_NewSongs *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewNewsSongs];
        cell.selectionStyle = NO;
        DataModel *dataModel = _arrTitleName[indexPath.section];
        cell.arrDatas = [dataModel.data mutableCopy];
        cell.backgroundColor = [UIColor blackColor];
        self.arrValue = [dataModel.data mutableCopy];
        _tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addtap:)];
        
        cell.imageNewsSongs.userInteractionEnabled = YES;
        [cell.imageNewsSongs addGestureRecognizer:_tap];
        _tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addtap:)];
        cell.imageNewsSongs1.userInteractionEnabled = YES;
        [cell.imageNewsSongs1 addGestureRecognizer:_tap1];
        _tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addtap:)];
        cell.imageNewsSongs2.userInteractionEnabled = YES;
        [cell.imageNewsSongs2 addGestureRecognizer:_tap2];
        return cell;
    } else if (indexPath.section == 4 || indexPath.section == 5) {
        HX_PhoneSongs *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewPhoneSongs];
        cell.selectionStyle = NO;
        DataModel *dataModel = _arrTitleName[indexPath.section];
        cell.arrDatas = [dataModel.data mutableCopy];
        cell.backgroundColor = [UIColor blackColor];
        return cell;
    } else if (indexPath.section == 7){
        HX_SoleHome *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewHome];
        DataModel *dataModel = _arrTitleName[indexPath.section];
        cell.selectionStyle = NO;
        cell.dataSmallModel = dataModel.data[indexPath.row];
        cell.backgroundColor = [UIColor blackColor];
        _tableView.separatorStyle = 0;
        return cell;
    } else if (indexPath.section == 1){
        static NSString *ID = @"ce";
        HX_CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if (!cell) {
            cell = [[HX_CustomCell alloc] initWithStyle:3 reuseIdentifier:ID];
        }
        cell.selectionStyle = NO;
        cell.arrDataSource = _arrayAliXingQiu;
        cell.backgroundColor = [UIColor blackColor];
        return cell;
    } else {
        static NSString *ID = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:3 reuseIdentifier:ID];
        }
        return cell;
    }
}
- (void)addtap:(UITapGestureRecognizer *)tap {
    HX_DetailView *detail = [[HX_DetailView alloc] init];
    DataSmallModel *data = [[DataSmallModel alloc] init];
    if (tap == _tap) {
        data = [self.arrValue objectAtIndex:0];
    } else if (tap == _tap1) {
        data = [self.arrValue objectAtIndex:1];
    } else {
        data = [self.arrValue objectAtIndex:2];
    }
    detail.stringUrl = data.value;
    [self.navigationController pushViewController:detail animated:YES];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat sectionHeaderHeight = 50;
    if(scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0,0);
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    if (section == 3 || section == 5 || section == 6 || section == 7) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 0)];
        view.backgroundColor = [UIColor blackColor];
        UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 50)];
        CGRect frame = lable.frame;
        frame.size.width = [SelfSizeTools selfSizeHeightWithString:[_arrTitleName[section] name] Width:50 Font:[UIFont systemFontOfSize:20]];
        lable.frame = frame;
        lable.center = CGPointMake(kWidth / 2, lable.frame.size.height / 2);
        lable.font = [UIFont systemFontOfSize:20];
        lable.textAlignment = 1;
        lable.textColor = [UIColor whiteColor];
        lable.text = [_arrTitleName[section] name];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(kWidth - 100, 0, 100, lable.frame.size.height);
        [btn setTitle:@"更多 >" forState:UIControlStateNormal];
        [view addSubview:btn];
        [view addSubview:lable];
        return view;
    } else {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 0)];
        view.backgroundColor = [UIColor blackColor];
        UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 50)];
        CGRect frame = lable.frame;
        frame.size.width = [SelfSizeTools selfSizeHeightWithString:[_arrTitleName[section] name] Width:50 Font:[UIFont systemFontOfSize:20]];
        lable.frame = frame;
        lable.center = CGPointMake(kWidth / 2, lable.frame.size.height / 2);
        lable.font = [UIFont systemFontOfSize:20];
        lable.textAlignment = 1;
        lable.textColor = [UIColor whiteColor];
        lable.text = [_arrTitleName[section] name];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(kWidth - 100, 0, 100, lable.frame.size.height);
        [btn setTitle:@"更多 >" forState:UIControlStateNormal];
        //        [view addSubview:btn];
        [view addSubview:lable];
        return view;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0 || section == 1) {
        return 0;
    } else {
        return 50;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 0;
    } else if (indexPath.section == 2) {
        return 120;
    } else if (indexPath.section == 3 || indexPath.section == 6) {
        return 150;
    } else if (indexPath.section == 4 || indexPath.section == 5){
        return 300;
    } else if (indexPath.section == 7){
        return 100;
    } else {
        return 100;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
